package logic

import (
	"common/global"
	"common/model"
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	user "user_server/proto"
)

func (s *UserServer) GetUserDetail(ctx context.Context, req *user.UserDetailRequest) (*user.UserDetailResponse, error) {
	// 1. 查询用户基本信息
	var o model.User
	if err := global.Db.First(&o, req.UserId).Error; err != nil {
		return nil, status.Errorf(codes.NotFound, "user not found")
	}

	// 2. 获取用户总签到天数
	var totalSignDays int64
	if err := global.Db.Model(&model.SignRecord{}).Where("user_id = ?", req.UserId).Count(&totalSignDays).Error; err != nil {
		return nil, status.Errorf(codes.Internal, "failed to get sign records")
	}

	// 3. 构造响应
	return &user.UserDetailResponse{
		UserId:             int32(o.ID),
		Nickname:           o.Nickname,
		Tel:                o.Tel,
		Avatar:             o.Avatar,
		RealName:           o.RealName,
		IsVerified:         o.RealName != "" && o.CardId != "",
		CreatedAt:          o.CreatedAt.Format("2006-01-02 15:04:05"),
		SignDays:           int32(totalSignDays),
		ContinuousSignDays: int32(o.ContinuousSignDays),
		RetroactiveCards:   int32(o.RetroactiveCards),
		LastSignDate:       o.LastSignDate,
	}, nil
}
